package com.allwees.bs.module.oms.service.impl;

import com.allwees.bs.core.model.order.wf.order.EOrderItemState;
import com.allwees.bs.module.core.dynamic.data.DynamicInjectQuery;
import com.allwees.bs.module.oms.dao.OmsOrderDao;
import com.allwees.bs.module.oms.dao.OmsOrderItemDao;
import com.allwees.bs.module.oms.dao.OrderItemDao;
import com.allwees.bs.module.oms.dao.OrderSkuSnapDao;
import com.allwees.bs.module.oms.entity.OmsOrder;
import com.allwees.bs.module.oms.entity.OrderSkuSnap;
import com.allwees.bs.module.oms.req.query.OmsOrderQueryReq;
import com.allwees.bs.module.oms.service.OrderStatisticsService;
import com.allwees.bs.module.oms.vo.OrderWithStatVo;
import com.allwees.core.common.constant.EStatus;
import com.allwees.core.common.util.DateUtil;
import me.zhengjie.utils.StringUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class OrderStatisticsServiceImpl implements OrderStatisticsService {

    @Autowired
    private OrderItemDao orderItemDao;
    @Autowired
    private OmsOrderItemDao omsOrderItemDao;
    @Autowired
    private OmsOrderDao omsOrderDao;
    @Autowired
    private OrderSkuSnapDao orderSkuSnapDao;
    @Autowired
    private DynamicInjectQuery dynamicInjectQuery;

    @Override
    public Integer findWaitPayCount(OmsOrderQueryReq req) {
        return orderItemDao.findCountByState(EOrderItemState.CREATED);
    }

    @Override
    public Integer findWaitShipCount(OmsOrderQueryReq req) {
        return omsOrderItemDao.queryCountByStatus(EStatus.OMS_WAIT_MERCHANT_SHIP);
    }

    @Override
    public Integer findWaitReviewCount(OmsOrderQueryReq req) {
        return orderItemDao.findCountByState(EOrderItemState.RECEIVED);
    }

    @Override
    public Integer findRefundedCount(OmsOrderQueryReq req) {
        return omsOrderItemDao.queryCountByStatus(EStatus.OMS_REFUND);
    }

    @Override
    public BigDecimal findYesterdayDealAmt(OmsOrderQueryReq req) {
        Date dateTo = DateUtil.getPreDay(DateUtil.getDayLast(new Date()));
        return omsOrderItemDao.findYesterdayDealAmt(DateUtil.addHour(dateTo,-24), dateTo);
    }

    @Override
    public BigDecimal findTodayDealAmt(OmsOrderQueryReq req) {
        return omsOrderItemDao.findTodayDealAmt(DateUtil.truncateToDay());
    }

    @Override
    public BigDecimal findTotalDealAmt(OmsOrderQueryReq req) {
        return omsOrderItemDao.findTotalDealAmt();
    }

    @Override
    public BigDecimal findRefundedDealAmt(OmsOrderQueryReq req) {
        return omsOrderItemDao.findTodayRefundDealAmt(DateUtil.truncateToDay());
    }

    @Override
    public List<OrderWithStatVo> findWithStat(OmsOrderQueryReq req) {
        List<OmsOrder> orders = omsOrderDao.selectList(req.toQueryWrapper());
        if (CollectionUtils.isEmpty(orders)) {
            return new ArrayList<>(0);
        }
        List<OrderWithStatVo> vos = orders.stream().map(OrderWithStatVo::new).collect(Collectors.toList());

        Map<String, List<OrderSkuSnap>> skuSnapMap = orderSkuSnapDao.findByOrderUuids(
                orders.stream().map(OmsOrder::getUuid).collect(Collectors.toSet())
        ).stream().collect(Collectors.groupingBy(OrderSkuSnap::getOrderUuid));

        for (OrderWithStatVo vo : vos) {
            List<OrderSkuSnap> orderSkuSnaps = skuSnapMap.get(vo.getUuid());
            if(CollectionUtils.isEmpty(orderSkuSnaps)){
                continue;
            }
            Set<String> categoryFullNames = new HashSet<>();
            for (OrderSkuSnap skuSnap : orderSkuSnaps) {
                categoryFullNames.add(dynamicInjectQuery.findCategoryFullname(skuSnap.getCategoryNo()));
            }
            vo.setCategoryFullName(StringUtils.join(categoryFullNames,","));
        }
        return vos;
    }
}
